XPath is a language for addressing XML (extensible Markup Language) documents. XPath has been widely accepted in many environments, especially in database environments. In fact, query languages such as XSLT, SQLX, and XQuery include XPath as a sublanguage. Given the importance of XPath as a mechanism for querying and navigating data, it is important that the evaluation of XPath expressions on XML documents be as efficient as possible. Ideally, the evaluation algorithm will traverse the XML document as little as possible before returning the result of the query.
The evaluation of an XPath expression is defined in terms of a tree structure representation of an XML document. A tree is a data structure composed of nodes. One of the nodes is specially designated to be the root node. All nodes in the tree other than the root node have exactly one parent node in the tree. An XML document can be represented as a labeled tree whose nodes represent the structural components of the document—elements, text, attributes, comments, and processing instructions. Element and attribute nodes have labels derived from the corresponding tags in the document and there may be more than one node in the document with the same label. Parent-child edges in the tree represent the inclusion of the child component in its parent element, where the scope of an element is bounded by its start and end tags. The tree corresponding to an XML document is rooted at a virtual element, Root, which contains the document element. We will, henceforth, discuss XML documents in terms of their tree representation. One can define an arbitrary order on the nodes of a tree. One such order might be based on a left-to-right depth-first traversal of the tree, which, for a tree representation of an XML document, corresponds to the document order.
Given an order on a tree, we can define a notion of a forward and backward relation on a tree. A relation R is a forward relation if whenever two nodes x and y are related by R, it must be the case that x precedes y in the order on the tree. Similarly, a relation is a backward relation if whenever x is related to y, then it must be the case that x follows y in the order on the tree. For example, assuming the document order for a tree representation of an XML document, the child and descendant relations are both forward relations, whereas the parent and ancestor relations are both backward relations.
An XPath expression over the tree representation of an XML document is evaluated in terms of a context node. The context node is a node in the tree representation of the document. If the context node is the root node of the document, the XPath expression is said to be an absolute XPath expression, otherwise, it is known as a relative XPath expression. Starting at a context node, an XPath expression specifies the axis to search and conditions that the results should satisfy. For example, assume that the context node is an element node c in the tree representation of an XML document. The XPath expression descendant::x specifies that starting from c, search all descendants of c and return all element nodes with label x. In this expression, “descendant” is the axis that is searched. One can compose XPath expressions to form larger XPath expressions. For example, the XPath expression descendant::x/ancestor::y specifies that starting from the context node c, find all element nodes that are descendants of c with label x, and for each such node, find all ancestor nodes with label y.
In traditional XPath processing, the XML document, over which XPath expressions are to be evaluated, is processed and a tree representation is built in memory. In evaluating an XPath expression over this in-memory tree representation of an XML document, an XPath processor, such as Xalan, may make several passes over the XML document. In the worst case, the number of passes over the XML document may be exponential in the size of the XPath expression (Georg Gottlob, Christoph Koch, Reinhard Pichler: Efficient Algorithms for Processing XPath Queries. VLDB 2002: 95-106). In many circumstances, for example, for large XML documents stored on a disk in a database, these multiple traversals can be prohibitively expensive. Xalan is an XSLT processor for transforming XML documents into HTML, text, or other XML document types. It implements XSL Transformations (XSLT) Version 1.0 and XML Path Language (XPath) Version 1.0. It can be used from the command line, in an applet or a servlet, or as a module in other program.
To alleviate this problem, streaming XPath evaluation algorithms have been developed that return a query result after exactly one pass over an XML document or tree. A streaming XPath engine is structured as shown in FIG. 1. An XPath expression 111 is analyzed and represented as an automaton 103. The XPath engine 101 consumes events (for example, SAX events) produced by a parser 105, and for each event, the automaton 103 may make state transitions, and if necessary, store the element. At the end of processing the document 107 the XPath engine returns the list of elements 109 that are the result of the evaluation of the XPath expression 111.
Algorithms for processing streaming XPath expressions are generally limited to absolute XPath expressions containing only forward axes (child and descendant axes). Discussing a related technology, U.S. patent application Ser. No. 10/264,076 entitled “A Method for Streaming XPath Processing with Forward and Backward Axes” by Charles Barton, Philippe Charles, Deepak Goyal, and Mukund Raghavachari, Proceedings of IEEE International Conference on Data Engineering, March 2003, presents a novel modification allowing streaming algorithms to handled both forward and backward axes (parent and ancestor) efficiently. More information is detailed in “Streaming XPath Processing with Forward and Backward Axes” by Charles Barton, Philippe Charles, Deepak Goyal, Mukund Raghavachari, Marcus Fontoura, and Vanja Josifovski, cited above. A novel representation of a data structure called an X-DAG makes this possible. An X-DAG is a data structure in which all occurrences of backward axes are converted into forward constraints, thereby making streaming XPath processing possible.
A limitation of this streaming algorithm is that it, like other streaming algorithms, only handles absolute XPath expressions. In practice, however, relative XPath expressions are more prevalent. To improve the efficiency of XPath processing it is important to devise streaming techniques for evaluating relative XPath expressions with both forward and backward axes in at the most one traversal of an XML document or tree.
Current streaming algorithms always traverse the entire XML document (exactly once) to evaluate an XPath expression over an XML document. In many cases, however, by ordering the XML document appropriately, it is possible to minimize the amount of the document traversed. When a relative expression is evaluated with respect to the context node c, it is more likely that the nodes around c will be relevant to the result than nodes in the tree that are far away from c. By reordering the traversal of the tree so that such nodes are traversed first, one can minimize the number of nodes traversed in many cases. For example, for the XPath expression descendant::x evaluated with respect to the context node c, we would only like to traverse the descendants of c and avoid traversal of the rest of the XML document. In general, such reorderings must handle complex XPath expressions involving ancestor and descendant axes, and integrate any such reordering into the streaming algorithm in a clean manner so that the algorithm still functions correctly.